package cn.hnucm.service;

import cn.hncum.feign.clients.HbClient;
import cn.hncum.feign.clients.UserClient;
import cn.hnucm.bean.*;
import cn.hnucm.pojo.User;
import cn.hnucm.vo.Goods;
import cn.hnucm.vo.ReturnOrderInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * @Author:liweihua
 * @Description:
 * @Date: 21:32$ 2021/12/28/028$
 * @Modified:
 */
@Service("hbControllerService")
public class HbControllerService extends AbstractCenterControllerService{
    @Autowired
    UserClient userClient;
    @Autowired
    HbClient hbClient;

    private User user;

    private Goods goods;

    private String ip;

    @Override
    public void setUser() {
        List<TbUserBasis> userBasisById = userClient.getUserBasisById(goods.getUserId()+"",this.ip, goods.getUserId());
        User user1 = new User();
        user1.setUserPassword(userBasisById.get(0).getUserPayPassword());
        user1.setAsset(userBasisById.get(0).getUserAsset());
        user1.setId(goods.getUserId());
        this.user = user1;
    }

    @Override
    public void setGoods(Goods goods) {
        this.goods = goods;
    }

    @Override
    public boolean matchPassword() {
        if(goods.getUserPassword().equals(user.getUserPassword())){
            return true;
        }
        return false;
    }

    @Override
    public boolean verifyBalance() {
        try {
            List<TbHbBasis> hbInfoById = hbClient.getHbInfoById(goods.getGoodId());
            List<TbUserBasis> userBasisById = userClient.getUserBasisById(goods.getUserId() + "", ip, hbInfoById.get(0).getHbReleaseUserid());
            float goodsPrice = userBasisById.get(0).getUserAsset();
            if(goodsPrice >= hbInfoById.get(0).getHbReward()){
                return true;
            }
            return false;
        }catch (Exception e){
            e.printStackTrace();
            return false;
        }
    }

    @Override
    public boolean updateGoods() {
        TbHbRelation tbHbRelation = new TbHbRelation();
        tbHbRelation.setHbId(goods.getGoodId());
        tbHbRelation.setHbState(2);
        tbHbRelation.setHbAcceptUserid(goods.getUserId());
        boolean b = hbClient.addOrder(tbHbRelation);
        return b;
    }

    @Override
    public boolean rollbackUpdateGoods() {
        TbHbRelation tbHbRelation = new TbHbRelation();
        tbHbRelation.setHbId(goods.getGoodId());
        tbHbRelation.setHbState(1);
        tbHbRelation.setHbAcceptUserid(goods.getUserId());
        boolean b = hbClient.addOrder(tbHbRelation);
        return b;
    }

    @Override
    public TbGoodsOrder UpdateOrder() {
        TbHbRelation tbHbRelation = new TbHbRelation();
        tbHbRelation.setHbId(goods.getGoodId());
        tbHbRelation.setHbState(3);
        tbHbRelation.setHbAcceptUserid(goods.getUserId());
        boolean b = hbClient.addOrder(tbHbRelation);
        return null;
    }

    @Override
    public boolean rollbackUpdateOrder(int orderId) {
        TbHbRelation tbHbRelation = new TbHbRelation();
        tbHbRelation.setHbId(goods.getGoodId());
        tbHbRelation.setHbState(1);
        tbHbRelation.setHbAcceptUserid(goods.getUserId());
        boolean b = hbClient.addOrder(tbHbRelation);
        return b;
    }

    @Override
    public boolean updateUserBalance() {
        try {
            List<TbHbBasis> hbInfoById = hbClient.getHbInfoById(goods.getGoodId());
            List<TbUserBasis> userBasisById = userClient.getUserBasisById(goods.getUserId() + "", ip, goods.getUserId());
            float goodsPrice = hbInfoById.get(0).getHbReward();
            boolean b = userClient.updateUserBalanceById(goods.getUserId(), userBasisById.get(0).getUserAsset() - goodsPrice);
            return b;
        }catch (Exception e){
            e.printStackTrace();
            return false;
        }
    }

    @Override
    public boolean rollbackUserBalance() {

        return false;
    }

    @Override
    public ReturnOrderInfo purchase(Goods goods,String ip) {
        System.out.println(goods.getGoodId());
        this.goods = goods;
        this.ip = ip;
        this.setUser();
        ReturnOrderInfo orderInfo = new ReturnOrderInfo();
        if(this.matchPassword()){
            if(this.verifyBalance()){

                boolean b = this.updateGoods();
                orderInfo.setMsg("皇榜接收成功");
                return orderInfo;
            }else{
                orderInfo.setMsg("皇榜发布者的资金不足");
                return orderInfo;
            }
        }else{
            orderInfo.setMsg("用户密码不正确！");
            return orderInfo;
        }
    }

    @Override
    public ReturnOrderInfo confirm(int id,String ip) {
        return null;
    }

    @Override
    public int unConfirm(int id,String ip) {
        return 0;
    }
}
